package inet.app.view.dialog;

import inet.app.R;
import inet.app.common.OnLoginListener;
import inet.app.common.XoSoConstants;
import inet.app.model.object.Member;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class InfoMemberDialog extends DialogFragment {

	private Context mContext;
	private Member mMember;
	private OnLoginListener mLogoutListener;

	public InfoMemberDialog(Context context) {
		this.mContext = context;
	}

	public static InfoMemberDialog newInstance(Context context) {
		InfoMemberDialog infoDialog = new InfoMemberDialog(context);
		return infoDialog;
	}

	public InfoMemberDialog setMember(Member member) {
		this.mMember = member;
		return this;
	}
	
	public InfoMemberDialog setLogoutListener(OnLoginListener logoutListener) {
		this.mLogoutListener = logoutListener;
		return this;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(mContext, android.R.style.Theme_Dialog);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setCancelable(false);
		dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		dialog.setTitle("Thông tin tài khoản");
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.view_info_member, null);
		dialog.setContentView(view);
		dialog.getWindow().setTitleColor(XoSoConstants.Yellow1);
		dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.icon_registersc);
	
		if (mMember != null) {
			((TextView) view.findViewById(R.id.full_name)).setText(mMember
					.getFullname());
			((TextView) view.findViewById(R.id.user_name)).setText(mMember
					.getUsername());
			((TextView) view.findViewById(R.id.email)).setText(mMember
					.getEmail());
			String mobile = mMember.getMobile();
			if(!TextUtils.isEmpty(mobile) && mobile.startsWith("84")) {
				mobile = mobile.replaceFirst("84", "0");
			}
			((TextView) view.findViewById(R.id.mobile)).setText(mobile);
			((TextView) view.findViewById(R.id.gender)).setText((mMember
					.getGender() == Member.GENDER_MALE) ? "Nam" : "Nữ");
			((TextView) view.findViewById(R.id.birthday)).setText(mMember
					.getBirthday());
		}

		view.setVisibility((mMember != null) ? View.VISIBLE : View.GONE);

		view.findViewById(R.id.logout_btn)
				.setOnClickListener(mLogoutClickListener);

		return dialog;
	}

	View.OnClickListener mLogoutClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			if(mLogoutListener != null) {
				mLogoutListener.onLogout();
				InfoMemberDialog.this.dismiss();
			}
		}
	};

}
